//
//  FilterDataTableView.m
//  Toyota
//
//  Created by ld on 2024/6/26.
//

#import "FilterDataTableView.h"
#import "FilterDataTableCell.h"
#import "Definition.h"
#import "Helper.h"
@interface FilterDataTableView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation FilterDataTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _dateArray = [[NSMutableArray alloc] init];
        self.backgroundColor=[UIColor whiteColor];
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[FilterDataTableCell class] forCellReuseIdentifier:@"FilterDataTableCell"];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dateArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    FilterDataTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FilterDataTableCell"];
    if (!cell) {
        cell = [FilterDataTableCell sortCell];
    }
    if (indexPath.row == _selectedIndex) {
        cell.markView.hidden = NO;
        cell.textLabel.textColor = kColorE93F33;
    }else{
        cell.markView.hidden = YES;
        cell.textLabel.textColor = kColor141414;
    }
    cell.textLabel.text = _dateArray[indexPath.row];
    cell.tag = indexPath.row;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 47;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // reset other cells color
    for (NSInteger i = 0; i < _dateArray.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        FilterDataTableCell *cell = [tableView cellForRowAtIndexPath:path];
        cell.markView.hidden = YES;
        cell.textLabel.textColor = kColor141414;
    }
    
    FilterDataTableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.markView.hidden = NO;
    cell.textLabel.textColor = kColorE93F33;
    if (self.choiceBtnClick) {
        self.choiceBtnClick(_dateArray[indexPath.row]);
    }
    _selectedIndex = indexPath.row;
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        self.customHeight=0;
    }];
}

#pragma mark - 设置默认显示
- (void)setSelectedCell:(NSString *)selectedCell
{
    // 显示View之前会给sortView.selectedCell赋值 继而处理选中cell reload
    _selectedCell = selectedCell;
    // 处理默认显示
    
    _selectedIndex = [self.dateArray indexOfObject:_selectedCell];
    if ([_selectedCell isEqualToString:@""] || _selectedCell == nil){
        _selectedIndex = 0;
    }
    
    [self reloadData];
}

- (void)setDateArray:(NSArray *)dateArray
{
    _dateArray = dateArray;
    self.customHeight = MIN(_dateArray.count, 7) * 47;
}


@end
